//
//  PhotoVideoCell.m
//  ChatForHot
//
//  Created by FLY on 2018/10/15.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PhotoVideoCell.h"
#import <AVFoundation/AVFoundation.h>

@implementation PhotoVideoCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 4;
        
        [self addSubview:self.HOTCHATbackImgView];
        [self addSubview:self.HOTCHATwaitImgVIew];
        [self addSubview:self.HOTCHATlocakImgVIew];
        
        [self.HOTCHATbackImgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.HOTCHATwaitImgVIew mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.center.equalTo(self);
            make.height.width.mas_equalTo(25);
        }];
        
        [self.HOTCHATlocakImgVIew mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(20);
            make.center.equalTo(self);
        }];
    }
    return self;
}

//lazzy

- (UIImageView *)HOTCHATbackImgView{
    if (_HOTCHATbackImgView == nil) {
        _HOTCHATbackImgView = [[UIImageView alloc] init];
        _HOTCHATbackImgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _HOTCHATbackImgView;
}
- (UIImageView *)HOTCHATwaitImgVIew{
    if (_HOTCHATwaitImgVIew == nil) {
        _HOTCHATwaitImgVIew = [[UIImageView alloc] init];
        _HOTCHATwaitImgVIew.image = [UIImage imageNamed:@"waitplay"];
        _HOTCHATwaitImgVIew.highlightedImage = [UIImage imageNamed:@"waitplay_hig"];
        _HOTCHATwaitImgVIew.hidden = YES;
    }
    return _HOTCHATwaitImgVIew;
}
- (UIImageView *)HOTCHATlocakImgVIew{
    if (_HOTCHATlocakImgVIew == nil) {
        _HOTCHATlocakImgVIew = [[UIImageView alloc] init];
        _HOTCHATlocakImgVIew.image = [UIImage imageNamed:@"user_photolist_lock"];
    }
    return _HOTCHATlocakImgVIew;
}

- (void)setModel:(VPModel *)model{
    _model = model;
    if (model.isVideo) {
        self.HOTCHATwaitImgVIew.hidden = NO;
        self.HOTCHATlocakImgVIew.hidden = YES;
        //        [self.photo sd_setImageWithURL:[model.url thumbnailURL] placeholderImage:[UIImage imageNamed:@"blue"]];
        [self HOTCHATthumbnailVidoImg:model.url];
    }else{
        self.HOTCHATwaitImgVIew.hidden = YES;
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIHEADER,model.url];
        NSURL *imgURL = [NSURL URLWithString:urlStr];
        [self.HOTCHATbackImgView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"personal_atatar_but_h"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image == nil) {
                image = [UIImage imageNamed:@"personal_atatar_but_h"];
            }
            
            if (HOTCHATAPPINFO.isVIP.integerValue == 1) {
                self.HOTCHATlocakImgVIew.hidden = YES;
                self.HOTCHATbackImgView.image = image;
            }else{
                self.HOTCHATlocakImgVIew.hidden = NO;
                self.HOTCHATbackImgView.image = [image HOTCHATapplyLightEffect];
            }
            
            
        }];
    }
}
- (void)HOTCHATthumbnailVidoImg:(NSString *)url {
    self.HOTCHATbackImgView.image = [UIImage imageNamed:@"personal_atatar_but_h"];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",APIHEADER,url];
    NSURL *videoURL = [NSURL URLWithString:urlStr];
    
    YYImageCache *chches = [YYImageCache sharedCache];
    __block UIImage *thumbImg = [chches getImageForKey:videoURL.absoluteString];
    
    if (thumbImg == nil) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
            AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
            gen.appliesPreferredTrackTransform = YES;
            CMTime time = CMTimeMakeWithSeconds(1.0, 600);
            NSError *error = nil;
            CMTime actualTime;
            CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
            thumbImg = [[UIImage alloc] initWithCGImage:image];
            
            [chches setImage:thumbImg forKey:videoURL.absoluteString];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([url isEqualToString:self.model.url]) {
                    if (thumbImg) {
                        if (HOTCHATAPPINFO.isVIP.integerValue == 1) {
                            self.HOTCHATbackImgView.image = thumbImg;
                        }else{
                            self.HOTCHATbackImgView.image = [thumbImg HOTCHATapplyLightEffect];
                        }
                    }
                }
            });
        });
    }else{
        if ([url isEqualToString:self.model.url]) {
            if (HOTCHATAPPINFO.isVIP.integerValue == 1) {
                self.HOTCHATbackImgView.image = thumbImg;
            }else{
                self.HOTCHATbackImgView.image = [thumbImg HOTCHATapplyLightEffect];
            }
            [self.HOTCHATbackImgView setNeedsLayout];
        }
    }
    
    
}


@end
